package com.cruising.backend.controller;

import cn.hutool.core.util.StrUtil;
import com.cruising.backend.entity.Admin;
import com.cruising.backend.entity.Result;
import com.cruising.backend.entity.vo.AdminLoginVO;
import com.cruising.backend.service.AdminService;
import com.cruising.backend.utils.JwtUtil;
import com.cruising.backend.utils.ResultUtil;
import com.cruising.backend.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yezi
 * @since 2025-04-21
 */
@Slf4j
@RestController
@Validated
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    // 登录方法
    @PostMapping("/login")
    public Result<AdminLoginVO> login(@Pattern(regexp="^\\S{5,16}$")String username,
                                      @Pattern(regexp="^\\S{5,16}$")String password) {
        System.out.println("username: " + username);
        System.out.println("password: " + password);
        //根据账号查找管理员
        Admin loginAdmin = adminService.findByUsername(username);
        System.out.println("loginAdmin: " + loginAdmin);

        //判断账号是否存在
        if (loginAdmin == null) {
            return ResultUtil.fail("该用户不存在！");
        }
        //判断密码是否正确
        if(password.equals(loginAdmin.getPassword())) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginAdmin.getAdminId());
            claims.put("username", loginAdmin.getUsername());
            claims.put("role", loginAdmin.getRole());
            
            // 生成token，设置1小时过期
            String token = JwtUtil.genToken(claims, 1000L * 60 * 60);
            
            // 获取角色信息
            String role = loginAdmin.getRole();
            Integer roleCode = Integer.parseInt(role);
            
            // 创建返回对象
            AdminLoginVO loginVO = new AdminLoginVO(token, roleCode);
            
            // 存储到Redis，设置过期时间
            ValueOperations<String, String> Operations = stringRedisTemplate.opsForValue();
            Operations.set(token, token, 1, TimeUnit.HOURS);
            
            return ResultUtil.success(loginVO);
        }

        return ResultUtil.fail("密码错误");
    }

    //查看管理员个人信息
    @GetMapping("/getAdminInfo")
    public Result<Admin> getAdminInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username=(String)map.get("username");
        Admin admin = adminService.findByUsername(username);
        if (admin != null) {
            return ResultUtil.success(admin);
        }
        return ResultUtil.fail("用户不存在");
    }


}
